package com.zbu.system.service.sysuser.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbu.common.core.constants.HttpConstants;
import com.zbu.common.core.domain.LoginUser;
import com.zbu.common.core.domain.vo.LoginUserVO;
import com.zbu.common.security.exception.ServiceException;
import com.zbu.common.security.service.TokenService;
import com.zbu.common.core.domain.R;
import com.zbu.common.core.enums.ResultCode;
import com.zbu.common.core.enums.UserIdentity;
import com.zbu.common.redis.service.RedisService;
import com.zbu.system.domain.sysuser.SysUser;
import com.zbu.system.domain.sysuser.dto.LoginDTO;
import com.zbu.system.domain.sysuser.dto.SysUserSaveDTO;
import com.zbu.system.mapper.sysuser.SysUserMapper;
import com.zbu.system.service.sysuser.ISysUserService;
import com.zbu.system.utils.BCryptUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@Service
@RefreshScope
public class SysUserServiceImpl implements ISysUserService {
    @Resource
    SysUserMapper sysUserMapper;
    @Autowired
    RedisService redisService;
    @Value("${jwt.secret}")
    private String secret;
    @Resource
    TokenService tokenService;
    @Override
    public R<String> login(LoginDTO loginDTO) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        SysUser sysUser=sysUserMapper.selectOne(lambdaQueryWrapper.
                select(SysUser::getUserId,SysUser::getPassword,SysUser::getNickName).
                eq(SysUser::getUserAccount,loginDTO.getUserAccount()));
        R loginResult=new R();
        if(sysUser==null){
            loginResult.setCode(ResultCode.FAILED_USER_NOT_EXISTS.getCode());
            loginResult.setMsg("用户不存在");
            return loginResult;
        }
        if(BCryptUtils.matchesPassword(loginDTO.getPassword(),sysUser.getPassword())){
            loginResult.setCode(ResultCode.SUCCESS.getCode());
            loginResult.setMsg(ResultCode.SUCCESS.getMsg());
            //过期时间怎么记录 过期时间设置为多长
            return R.ok(tokenService.createToken(sysUser.getUserId(),secret,UserIdentity.ADMIN.getValue(),sysUser.getNickName(),null));
        }
        loginResult.setCode(ResultCode.FAILED_LOGIN.getCode());
        loginResult.setMsg(ResultCode.FAILED_LOGIN.getMsg());
        return R.fail(ResultCode.FAILED_LOGIN);
    }

    @Override
    public boolean logout(String token) {
        if (StrUtil.isNotEmpty(token) &&
                token.startsWith(HttpConstants.PREFIX)) {
            token = token.replaceFirst(HttpConstants.PREFIX, StrUtil.EMPTY);
        }
        return tokenService.deleteLoginUser(token,secret);

    }
    @Override
    public R<LoginUserVO> info(String token) {
        if (StrUtil.isNotEmpty(token) &&
                token.startsWith(HttpConstants.PREFIX)) {
            token = token.replaceFirst(HttpConstants.PREFIX, StrUtil.EMPTY);
        }
        LoginUser loginUser=tokenService.getLoginUser(token,secret);
        if(loginUser==null){
            return R.fail();
        }
        LoginUserVO loginUserVO=new LoginUserVO();
        loginUserVO.setNickName(loginUser.getNickName());
        return R.ok(loginUserVO);
    }
    @Override
    public int  add(SysUserSaveDTO sysUserSaveDTO) {
        if(!userIsempty(sysUserSaveDTO)){
            //自定义异常
            throw new ServiceException(ResultCode.AILED_USER_EXISTS);
        }
        SysUser sysUser=new SysUser();
        sysUser.setUserAccount(sysUserSaveDTO.getUserAccount());
        sysUser.setPassword(BCryptUtils.encryptPassword(sysUserSaveDTO.getPassword()));
        return sysUserMapper.insert(sysUser);

    }



    public Boolean userIsempty(SysUserSaveDTO sysUserSaveDTO){
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        SysUser sysUser=sysUserMapper.selectOne(lambdaQueryWrapper.
                select(SysUser::getUserId, SysUser::getPassword).
                eq(SysUser::getUserAccount,sysUserSaveDTO.getUserAccount()));
        if(sysUser==null){
            return true;
        }
        return false;
    }

}
